<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;
use think\Exception;//事务处理

class Coattr extends Model
{
    public function getcoattr($res)
    {
        $langs = $where['a.lang'] = $res['lang'];//语言标志
        $join = [
            ['nt_coattr b', 'a.mkey=b.mkey']
        ];
        $table = 'multi_lang';
        $pagelimit['limit'] = $res['pageSize'];
        $pagelimit['start'] = ($res['page'] - 1) * $res['pageSize'];
        $datares = getmultidata($table, $join, $where, $pagelimit, null);//获取的是对应的语言属性名称
        foreach ($datares['data'] as $datareskey => $dataresval) {
            //获取对应的语言公司类型名称
            $datares['data'][$datareskey]['cotypename'] = $this->getcotype($dataresval['cotype_ids'], $langs);
            if ($dataresval['is_text'] == 1) {
                $datares['data'][$datareskey]['textselect'] = lang("select_box");
            } else {
                $datares['data'][$datareskey]['textselect'] = lang("text");
            }
            $datares['data'][$datareskey]['country'] = getcountry($dataresval['country_id']);//国家
            if ($dataresval['person'] == 1) {
                //适用个人
                $datares['data'][$datareskey]['personman'] = '+';
            } else {
                $datares['data'][$datareskey]['personman'] = '-';
            }
            //编辑时间
                $datares['data'][$datareskey]['update_date'] = getTimeDate($dataresval['update_at'],12);
            //编辑者
            $datares['data'][$datareskey]['mangname'] = getinfo($dataresval['update_id'], 'admin')['nickname'];//编辑者
        }
        $data['data'] = $datares['data'];
        $data['count'] = $datares['count'];
        return $data;
    }

    //获取对应的公司类型的名称
    protected function getcotype($cotype_ids, $langs)
    {
        $cotypearr = explode(',', $cotype_ids);
        foreach ($cotypearr as $cotypearrkey => $cotypearrval) {
            $where['mkey'] = 'cotype_' . $cotypearrval;
            //对应下的语言名称
            $cotypname[] = getlangname('multi_lang', $where, $langs);
        }
        return implode(' / ', $cotypname);
    }

    //公司属性的添加编辑
    public function addeditcoattr($data)
    {
        if ($data['act'] == 'coattradd') {
            $ckdata = notEmpty(['coattrname' => lang("attribute_name")]);
        } else {
            $dname_zh = trim($data['lang_zh']);//中文
            $dname_en = trim($data['lang_en']);//英文
            $dname_pt = trim($data['lang_pt']);//葡萄牙文
            $ckdata = notEmpty(['lang_zh' => lang("Chinese_attribute_name"), 'lang_en' => lang("English_attribute_name"), 'lang_pt' => lang("Portuguese_attribute_name")]);
        }
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        if (!isset($data['cotype_ids'])) {
            return array('msg' => lang("company_typo_can't_be_empty"), 'status' => 2);
        }
        $lang = $data['langs'];
        unset($data['langs']);
        //公司类型
        $datadepart['cotype_ids'] = $data['cotype_ids'] = implode(',', $data['cotype_ids']);
        if ($data['is_text'] == 1) {
            if (!isset($data['option_text'])) {
                return array('msg' => lang("select_box_can't_be_empty"), 'status' => 2);
            }
            if (in_array('', $data['option_text'])) {
                return array('msg' => lang("select_box_can't_contain_null_values"), 'status' => 2);
            }
            $countone = count(array_filter($data['option_text']));//去空后的个数
            $counttwo = count(array_unique(array_filter($data['option_text'])));//去重去空后的个数
            if ($countone != $counttwo) {
                return array('msg' => lang("select_box_contains_duplicate_values"), 'status' => 2);
            }
        }
        if ($data['act'] == 'coattradd') {
            unset($data['id']);
            $depalike = 'coattr';
            $langwhere['mkey'] = array('like', $depalike . '%');
            $coattrname = $langwhere['content'] = trim($data['coattrname']);
            $langzh = getables('multi_lang', $langwhere);
            if ($langzh) {
                return array('msg' => lang("company_attribute_exists"), 'status' => 3);
            }
            $data['create_at'] = $data['update_at'] = time();
            $data['create_id'] = session('userid');
            //开启事务
            Db::startTrans();
            $coattrid = $this->strict(false)->insertGetId($data);//公司属性表
            $coattrkey = 'coattr_' . $coattrid;
            //更新数据
            $this->where('id', $coattrid)->setField('mkey', $coattrkey);
            $langdata = getables('language');
            $multidata = array();
            foreach ($langdata as $langdatakey => $langdataval) {
                $multidata[$langdatakey]['content'] = $coattrname;
                $multidata[$langdatakey]['mkey'] = $coattrkey;
                $multidata[$langdatakey]['lang'] = $langdataval['code'];
            }
            $langid = Db::name('multi_lang')->insertAll($multidata);//语言表
            //select表
            if ($data['is_text'] == 1) {
                $rescoattr = [];
                if ($counttwo) {
                    for ($i = 0; $i < $counttwo; $i++) {
                        $row = [
                            'coattr_id' => $coattrid,//属性的id
                            'option_text' => $data['option_text'][$i],//所属公司类型
                        ];
                        $rescoattr[] = $row;
                    }
                    //选择框中的值
                    $resids = Db::name('coattr_select')->insertAll($rescoattr);
                }
            } else {
                $resids = 1;
            }

            if ($coattrid && $langid && $resids) {
                Db::commit();
                return array('msg' => $coattrname . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $coattrname . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'coattredit') {
            switch ($lang) {
                case 'zh':
                    $langedit = $dname_zh;
                    break;
                case 'en':
                    $langedit = $dname_en;
                    break;
                case 'pt':
                    $langedit = $dname_pt;
                    break;
                default:
                    $langedit = $dname_zh;
                    break;
            }
            //开启事务
            Db::startTrans();
            //删除select的选择框
            Db::name('coattr_select')->where('coattr_id', $data['id'])->delete();
            $datadepart['update_at'] = time();
            $datadepart['is_text'] = $data['is_text'];
            $datadepart['country_id'] = $data['country_id'];
            $datadepart['person'] = $data['person'];
            $datadepart['update_id'] = session('userid');
            //更新属性表
            $resid = $this->where('id', $data['id'])->update($datadepart);
            //更新语言表
            $departmkey = 'coattr_' . $data['id'];
            $langupidzh = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'zh'])->setField('content', $dname_zh);//更新中文公司属性名称
            $langupiden = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'en'])->setField('content', $dname_en);//更新英文公司属性名称
            $langupidpt = Db::name('multi_lang')->where(['mkey' => $departmkey, 'lang' => 'pt'])->setField('content', $dname_pt);//更新葡文公司属性名称
            if ($data['is_text'] == 1) {
                //更新select框的值
                if ($counttwo) {
                    for ($i = 0; $i < $counttwo; $i++) {
                        $row = [
                            'coattr_id' => $data['id'],//属性的id
                            'option_text' => $data['option_text'][$i],//所属公司类型
                        ];
                        $rescoattr[] = $row;
                    }
                    $resids = Db::name('coattr_select')->insertAll($rescoattr);
                }
            } else {
                $resids = 1;
            }
            if ($resid !== false && $langupidzh !== false && $langupiden !== false && $langupidpt !== false && $resids) {
                Db::commit();
                return array('msg' => $langedit . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $langedit . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //公司属性的删除
    public function delcoattr($data, $code)
    {
        $lang = $data['langs'];
        //查询具体语言下的部门
        $coattrwhere['mkey'] = 'coattr_' . $data['id'];
        $langname = getlangname('multi_lang', $coattrwhere, $lang);
        $coattr = Db::name('coattr_value')->where('coattr_id', $data['id'])->select();
        if ($coattr) {
            return array('msg' => $langname . lang("contains_corresponding_company_and_can't_be_deleted"), 'status' => 0);
        }
        if ($code == 1) {
            $updelete['del_at'] = time();
            $updelete['del_id'] = session('userid');
            $retrole = $this->where('id', $data['id'])->update($updelete);//属性表
        } else {
            $retrole = $this->where('id', $data['id'])->delete();
            //删除语言表
            Db::name('multi_lang')->where('mkey', 'coattr_' . $data['id'])->delete();
            //删除属性表
            Db::name('coattr_select')->where('coattr_id', $data['id'])->delete();
        }
        if ($retrole !== false) {
            return array('msg' => $langname . lang("deleted_successfully"), 'status' => 1);
        } else {
            return array('msg' => $langname . lang("failed_to_delete"), 'status' => 0);
        }
    }
}


